<template>
	<view>
		<!-- 一行两列商品展示 -->
		<view class="goods-list" v-if="type == 'twoColumns'">
			<view v-for="(item, index) in res" :key="index" class="goods-item">
				<view class="image-wrapper" @click="navigateToDetailPage(item)">
					<u-image :src="item.image" width="100%" height='330rpx' mode="aspectFit">
						<u-loading slot="loading"></u-loading>
					</u-image>
				</view>
				<view class="goods-detail">
					<div class="title clamp" @click="navigateToDetailPage(item)">
						{{ item.name}}
					</div>


					<view class="price-box" @click="navigateToDetailPage(item)">
						<div class="price" v-if="item.price!=undefined">
							¥<span>{{ item.price}}</span>
						</div>
						<div class="yuexiao">月销: {{item.sellNum}}</div>
					</view>
					<!-- <view class="car" @click.stop="showProductguige(item)">
						<u-image width="100%" height="100%" src="/static/newImg/car.png"></u-image>
					</view> -->
				</view>
			</view>
		</view>


		<u-popup v-model="carshow" mode="bottom" :closeable="true" :mask-close-able="false">
			<view class="carshowview">
				<view class="goods-header">
					<view class="goods-img">
						<u-image width="178rpx" class="uimage" height="178rpx" :src="currentProduct.image"></u-image>
					</view>

					<view class="goods-price">
						<span class="lsj">零售价 </span>
						<span class="price">{{currentProduct.price}}</span>
					</view>

					<view class="goods-kc">
						<span>库存1531件</span>
					</view>

					<view class="goods-spgg"> 请选择 商品规格 </view>
				</view>

				<view class="goods-sku">
					<view class="skutitle">商品规格</view>

					<view class="skulist">
						<view class="skulistitem" v-for="item in skulist" :key="item.id"
							:class="item.id == currentskuid ? 'skulight' : ''" @click="handleskuclick(item)">
							{{ item.name }}
						</view>
					</view>
				</view>

				<view class="numbox">
					<view class="numboxtitle">购买数量</view>
					<uni-number-box class="uNumber" :min="0" :max="999" v-model="num"></uni-number-box>
				</view>

				<view class="btns">
					<view class="box-btn-card" @click="addToCartOrBuy('cart')">加入购物车</view>
					<view class="box-btn-buy" @click="addToCartOrBuy('buy')">立即购买</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import commonTpl from '@/components/m-goods-list/common';
	import {
		productSpecifications
	} from '@/api/goods.js';
	import uniNumberBox from "@/components/uni-number-box";
	export default {
		data() {
			return {
				lightColor: this.$mainColor,
				skulist: [],
				currentProduct: {},
				carshow: false,
				num: 1,
				currentskuid: 1,
			}
		},
		components: {
			uniNumberBox,
		},
		mixins: [commonTpl],

		props: {
			// 遍历的数据
			res: {
				type: Array,
				default: () => {
					return []
				}
			},
			// 一行两列还是一行一列显示
			type: {
				type: String,
				default: 'twoColumns',
				validator() {
					return ['twoColumns', 'oneColumns']
				}
			},
			storeName: {
				type: Boolean,
				default: true
			},
			keyword: {
				type: null,
				default: ''
			},


		},
		watch: {
			keyword(val) {
				if (val) {
					this.lightSearchStr(val)
				}
			}
		},
		methods: {


			handleskuclick({
				id
			}) {
				this.currentskuid = id;
			},

			showProductguige(product) {

				uni.showLoading({
					title: "加载中",
				});
				this.currentProduct = {
					...product
				};

				productSpecifications({
					id: product.id
				}).then(res => {

					uni.hideLoading();
					this.skulist = res.data || []
					this.carshow = true;
				})



			},

			// 高亮显示搜索内容	
			lightSearchStr(keyword, str) {
				if (!keyword) {
					return str
				} else {
					let unicodes = '';
					for (let i of Array.from(keyword)) {
						unicodes += this.unicode(i) + "|"
					}
					const rule = '(' + unicodes + ')'
					const reg = new RegExp(rule, 'gi');
					return str ? str.replace(reg, matchValue =>
						`<span style="color:${this.lightColor}">${matchValue}</span>`
					) : ''
				}
			},
			//  转换为unicode
			unicode(str) {
				var value = '';
				for (var i = 0; i < str.length; i++) {
					value += '\\u' + this.left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
				}
				return value;
			},
			left_zero_4(str) {
				if (str != null && str != '' && str != 'undefined') {
					if (str.length == 2) {
						return '00' + str;
					}
				}
				return str;
			},
			// 数据去重一下 只显示一次 减免 劵 什么的
			getPromotion(item) {
				if (item ? item.promotionMap : item.promotionMap) {
					const fieldList = item ? item.promotionMap : item.promotionMap
					let array = [];
					Object.keys(fieldList).forEach((child) => {
						if (!array.includes(child.split("-")[0])) {
							array.push(child.split("-")[0]);
						}
					});
					return array;
				}
			},
			// 跳转到商品详情
			navigateToDetailPage(item) {
				uni.navigateTo({
					url: `/pages/product/goods?id=${item.id}`,
				});
			},
			// 跳转地址
			navigateToStoreDetailPage(item) {
				uni.navigateTo({
					url: `/pages/product/shopPage?id=${item.storeId}`,
				});
			},
		}
	}
</script>

<style lang='scss' scoped>
	.goods-one-row {
		padding-bottom: 250rpx;
	}

	/* 商品列表 */
	.goods-list {
		display: flex;
		flex-wrap: wrap;
		margin: 10rpx 20rpx 284rpx;
		width: 100%;

		>.goods-item {
			background-color: #ffffff;
			display: flex;
			border-radius: 16rpx;
			flex-direction: column;
			width: 345rpx;
			margin-bottom: 20rpx;
			padding-bottom: 20rpx;
			position: relative;

			&:nth-child(2n + 1) {
				margin-right: 20rpx;
			}


			.image-wrapper {
				width: 100%;
				height: 330rpx;
				border-radius: 16rpx 16rpx 0 0;
				overflow: hidden;
				padding: 0;
			}
		}

		.count-config,
		.store-seller-name {
			font-size: $font-sm;
		}

		.text-hidden {
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
	}


	.goods-row {
		background: #fff;
		padding: 16rpx;

		>.goods-col {
			display: flex;

			>.goods-img {
				overflow: hidden;
				flex: 4;
			}

			>.goods-detail {
				flex: 7;
			}
		}
	}

	.goods-detail {
		margin: 0 20rpx;
		position: relative;
		height: 135rpx;

		>.title {
			width: 100%;
			font-family: PingFang;
			font-weight: 500;
			font-size: 27rpx;
			color: #0D182E;
			overflow: hidden;
			position: absolute;
			top: 21rpx;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}

		.yuexiao {}

		.promotion {
			margin-top: 4rpx;
			display: flex;

			div {
				span {
					font-size: 24rpx;
					color: $light-color;
					margin-right: 10rpx;
					padding: 0 4rpx;
					border-radius: 2rpx;
				}
			}
		}

		.car {
			width: 42rpx;
			height: 41rpx;
			position: absolute;
			bottom: 20rpx;
			right: 20rpx;
		}

		.store-seller-name {
			color: #666;
			overflow: hidden;
			display: flex;
			justify-content: space-between;
		}

		.count-config {
			padding: 5rpx 0;
			color: #666;
			display: flex;
			font-size: 24rpx;
			justify-content: space-between;
		}

		>.price-box {
			display: flex;
			align-items: flex-end;
			justify-content: space-between;
			padding-right: 10rpx;
			font-size: 23rpx;
			color: #FF3A3A;

			position: absolute;
			top: 78rpx;

			>.price {
				font-size: 23rpx;
				line-height: 1;
				color: #FF3A3A;

				/deep/ span:nth-of-type(1) {
					font-size: 38rpx;
				}
			}

			.yuexiao {
				font-family: Source Han Sans CN;
				font-weight: 400;
				font-size: 18rpx;
				color: #BCC1CA;
				padding-left: 10rpx;
			}
		}

	}

	.carshowview {
		width: 100%;
		height: 840rpx;
		overflow-y: auto;
		position: relative;

		.goods-header {
			width: 100%;
			position: relative;
			height: 233rpx;
			border-bottom: 1px solid #f3f3f3;
		}

		.goods-img {
			width: 178rpx;
			height: 178rpx;
			position: absolute;
			top: 21rpx;
			left: 21rpx;
		}

		.goods-price {
			position: absolute;
			top: 42rpx;
			left: 223rpx;
		}

		.lsj {
			font-family: Microsoft YaHei;
			font-weight: 400;
			font-size: 28rpx;
			color: #232323;
			line-height: 50rpx;
		}

		.price {
			font-family: Microsoft YaHei;
			font-weight: bold;
			font-size: 35rpx;
			color: #f76e00;
			padding-left: 20rpx;
		}

		.goods-kc {
			font-family: Microsoft YaHei;
			font-weight: 400;
			font-size: 28rpx;
			color: #232323;
			opacity: 0.7;
			position: absolute;
			top: 93rpx;
			left: 224rpx;
		}

		.goods-spgg {
			font-family: Microsoft YaHei;
			font-weight: 400;
			font-size: 28rpx;
			color: #232323;
			line-height: 44rpx;
			position: absolute;
			top: 140rpx;
			left: 224rpx;
		}

		.goods-sku {
			width: 100%;
			height: 375rpx;
			overflow-y: auto;
			border-bottom: 1px solid #f3f3f3;
			position: relative;
		}

		.skutitle {
			font-family: Microsoft YaHei;
			font-weight: 400;
			font-size: 28rpx;
			color: #232323;

			padding-top: 24rpx;
			padding-left: 26rpx;
		}

		.skulist {
			padding-left: 26rpx;
			margin-top: 29rpx;
			display: flex;
			justify-content: flex-start;
			flex-wrap: wrap;
			align-content: flex-start;
		}

		.skulistitem {
			min-width: 254rpx;
			height: 70rpx;
			line-height: 70rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			font-size: 25rpx;
			color: #666666;
			cursor: pointer;
			border: 1px solid #f3f3f3;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			padding: 0 20rpx;
		}

		.skulight {
			border: 1px solid rgb(25, 189, 93);
		}
	}

	.btns {
		width: 680rpx;
		height: 87rpx;
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		font-family: PingFang;
		font-weight: bold;
		font-size: 28rpx;
		color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: space-between;

		bottom: 15rpx;
	}

	.box-btn-card {
		height: 100%;
		width: 50%;
		background-color: rgb(245, 181, 64);
		line-height: 87rpx;
		text-align: center;
		border-radius: 40rpx 0 0 40rpx;
	}

	.box-btn-buy {
		height: 100%;
		width: 50%;
		background-color: rgb(103, 194, 66);
		line-height: 87rpx;
		text-align: center;
		border-radius: 0 40rpx 40rpx 0;
	}

	.numbox {
		width: 700rpx;
		margin: 0 auto;
		margin-top: 28rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.numboxtitle {
		font-family: Microsoft YaHei;
		font-weight: 400;
		font-size: 28rpx;
		color: #232323;
	}
</style>
